﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;

namespace TestMongoDb
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 新增
            //TestInsert test=new TestInsert();
            //test.RuntoMongoDb_InsertOne();
            //test.RuntoMongoDb_InsertMore();
            #endregion

            #region 删除
            //TestDelete test2=new TestDelete();
            //test2.RuntoMongoDb_DeleteOne();
            //test2.RuntoMongoDb_DeleteMore();
            #endregion

            #region 修改

            //TestUpdate test3=new TestUpdate();
            //test3.RuntoMongoDb_UpdateOne();
            //test3.RuntoMongoDb_UpdateMore();

            #endregion 

            #region 查询
            TestFind test4 = new TestFind();
            test4.RuntoMongoDb_FindOne();
            test4.RuntoMogoDb_FindOne_Project();
            test4.RuntoMogoDb_FindOne_Sort();
            test4.RuntoMogoDb_FindMore();
            #endregion
        }
    }

    /// <summary>
    /// 测试插入（新增）
    /// </summary>
    public class TestInsert
    {
        MongoDbHelper _mongoDbHelper = new MongoDbHelper("test", "Product");

        //插入一条
        public void RuntoMongoDb_InsertOne()
        {
            Person person = new Person()
            {
                name = "zhangsan",
                age = 16,
                dept = 1
            };
            _mongoDbHelper.InsertOne(person.ToBsonDocument());
        }

        //插入多条
        public void RuntoMongoDb_InsertMore()
        {
            List<BsonDocument> persons = new List<BsonDocument>();
            for (int i = 0; i < 5; i++)
            {
                Person person = new Person()
                {
                    name = "zhangsan" + i,
                    age = 16 + i,
                    dept = 2
                };
                persons.Add(person.ToBsonDocument());
            }

            _mongoDbHelper.InsertManay(persons);
        }
    }

    /// <summary>
    /// 测试删除
    /// </summary>
    public class TestDelete
    {
        MongoDbHelper _mongoDbHelper = new MongoDbHelper("test", "Product");

        //删除一条
        public void RuntoMongoDb_DeleteOne()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("name", "zhangsan");
            var result = _mongoDbHelper.DeleteOne(filterDoc);
        }

        //删除多条
        public void RuntoMongoDb_DeleteMore()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("dept", 2);
            var result = _mongoDbHelper.DeleteMore(filterDoc);
        }
    }

    /// <summary>
    /// 测试更新
    /// </summary>
    public class TestUpdate
    {
        MongoDbHelper _mongoDbHelper = new MongoDbHelper("test", "Product");

        //更新一条
        public void RuntoMongoDb_UpdateOne()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("name", "bj");
            var updateDoc = Builders<BsonDocument>.Update.Set("name", "bjt");
            var result = _mongoDbHelper.Update(filterDoc, updateDoc);
        }

        //更新多条
        public void RuntoMongoDb_UpdateMore()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("dept", 1);
            var updateDoc = Builders<BsonDocument>.Update.Set("dept", 2);
            var result = _mongoDbHelper.UpdateMore(filterDoc, updateDoc);
        }
    }

    /// <summary>
    /// 测试搜索
    /// </summary>
    public class TestFind
    {
        MongoDbHelper _mongoDbHelper = new MongoDbHelper("test", "Product");

        //查询一条
        public void RuntoMongoDb_FindOne()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("name", "bjt");
            var result = _mongoDbHelper.FindOne(filterDoc);

        }

        //查询--指定字段
        public void RuntoMogoDb_FindOne_Project()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("name", "bjt");
            var prjectDoc = Builders<BsonDocument>.Projection.Include("name").Exclude("_id").Include("age");
            var result = _mongoDbHelper.FindOne(filterDoc, null, prjectDoc);
            var resultEntity = BsonSerializer.Deserialize<Person>(result);//转换成实体
        }

        //查询--排序
        public void RuntoMogoDb_FindOne_Sort()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("dept", 2);
            var sortDoc = Builders<BsonDocument>.Sort.Descending("age");
            var result = _mongoDbHelper.FindOne(filterDoc, sortDoc, Builders<BsonDocument>.Projection.Exclude("_id"));
            var resultEntity = BsonSerializer.Deserialize<Person>(result);
        }

        //查询多条
        public void RuntoMogoDb_FindMore()
        {
            var filterDoc = Builders<BsonDocument>.Filter.Eq("dept", 2);
            var result = _mongoDbHelper.FindMore(filterDoc);

        }
    }

    public class Person
    {
        public string name { get; set; }

        public int age { get; set; }

        public int dept { get; set; }
    }
}
